/*****************
*@file: hander.go
*@author: jackwong
*@brief:  与web通信接口
*@version: 1.0
*@date: 2022/4/28 16:18
*****************/
package hanlder

import (
	"net/http"
	"time"

	"github.com/sirupsen/logrus"
)

type HttpServer interface {
	Start()
	Stop()
}
type httpServer struct {
}

func NewServer() HttpServer {
	return &httpServer{}
}

var server *http.Server

func (p *httpServer) Start() {
	http.Handle("/login", new(loginHandler))
	http.Handle("/getuserinfo", new(getUserInfoHandler))
	http.Handle("/query", new(queryHandler))
	http.Handle("/play/", new(playerHandler))
	http.Handle("/download", new(downloadHandler))
	http.Handle("/delete", new(deleteHandler))
	http.Handle("/getCarList", new(getCarListHandler))
	http.Handle("/insert", new(insertHandler))
	http.Handle("/", new(downloadUrlHandler))
	http.Handle("/ws", new(webSocketHandler)) //创建webSocket句柄
	server = &http.Server{
		Addr:           ":8080",
		Handler:        http.DefaultServeMux,
		ReadTimeout:    10 * time.Minute,
		WriteTimeout:   10 * time.Minute,
		MaxHeaderBytes: 1 << 20,
	}

	server.ListenAndServe()
	logrus.Debug("Http Server Running")
}

func (p *httpServer) Stop() {
	server.Shutdown(nil)
}
